package com.tql.view.controller;

import com.tql.view.pojo.Logs;
import com.tql.view.pojo.Notice;
import com.tql.view.pojo.User;
import com.tql.view.service.LogsService;
import com.tql.view.service.NoticeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class LoginController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private LogsService logsService;
    @RequestMapping("/toView")
    public String toView(HttpSession session){
        Notice notice=noticeService.getLastOne();
        session.setAttribute("notice",notice);
        return "view";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(@RequestParam("account") String account, @RequestParam("password")String password, Model model, HttpSession session1){
        Subject subject= SecurityUtils.getSubject();
        //获取登录令牌
        UsernamePasswordToken token=new UsernamePasswordToken(account,password);
        try {
            String url="";
            //登录跳转shiro过滤处理
            subject.login(token);
          /*  if(subject.hasRole("amdin")){
                return "admin/admin";
            }*/
            //判断用户角色，进行制定跳转
            Session session=subject.getSession();
            User user= (User) session.getAttribute("user");
            if(user.getRole().equals("admin")){
                session1.setAttribute("visitor",user);
                url="admin/adminMain1";
            }
            if(user.getRole().equals("user")){
                Notice notice=noticeService.getLastOne();
                session.setAttribute("notice",notice);
                session1.setAttribute("visitor",user);
                url="user/userMain";
            }
            Logs logs=new Logs(user.getUsername(),user.getAccount(),"登录成功",new Date());
            logsService.insertLogs(logs);
            return url;
        }catch (UnknownAccountException e){
            Logs logs=new Logs("未知用户",account,"登录失败",new Date());
            logsService.insertLogs(logs);
            return "login";
        }catch (IncorrectCredentialsException e){
            Logs logs=new Logs("未知用户",account,"登录失败",new Date());
            logsService.insertLogs(logs);
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }
    @RequestMapping("/test")
    public String test(){
        return "view/test";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("visitor");
        session.removeAttribute("admin");
       /* Notice notice=noticeService.getLastOne();
        session.setAttribute("notice",notice);*/
        Subject subject=SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/toView";
    }
    /*
    * 关于界面
    * */
    @RequestMapping("/toConcern")
    public String toConcern(){return "concern";}
}
